package school.community.controller;

import school.community.dto.InfoDTO;
import school.community.enums.NotificationTypeEnum;
import school.community.mapper.InfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import school.community.model.User;
import school.community.service.InfoService;
import school.community.service.NotificationService;

import javax.servlet.http.HttpServletRequest;

@Controller
@Slf4j
public class DeleteController {
  @Autowired
  InfoMapper infoMapper;

  @Autowired
  private NotificationService notificationService;

  @Autowired
  private InfoService infoService;

  @GetMapping("/delete/{id}")
  public String delete(@PathVariable(name = "id") Long id,
                       HttpServletRequest request) {

    InfoDTO infoDTO = infoService.getById(id);
    //如果是管理员删除，则需要发一个通知给该用户

    User admin = (User) request.getSession().getAttribute("admin");
    if (admin != null) {
      notificationService.createNotify(3L,infoDTO.getCreator(),"admin",infoDTO.getId(),infoDTO.getTitle(), NotificationTypeEnum.DELETE_TOPIC);
    }
    //根据ID删除信息
    infoMapper.deleteByPrimaryKey(id);

    log.info("成功删除");
    return "redirect:/";
  }
}
